package com.fq.myapplication;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.fq.myapplication.fragment.FavFragment;
import com.fq.myapplication.fragment.HomeFragment;
import com.fq.myapplication.fragment.ProfileFragment;
import com.fq.myapplication.utils.ThemeManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // # 底部栏点击切换
        // - 1. 初始化 fragment
        switchFragment(HomeFragment.newInstance());
        // - 2. 设置底部导航栏点击事件
        TextView homeTextView = findViewById(R.id.home);
        TextView favTextView = findViewById(R.id.fav);
        TextView profileTextView = findViewById(R.id.profile);
        homeTextView.setOnClickListener(v -> switchFragment(HomeFragment.newInstance()));
        favTextView.setOnClickListener(v -> switchFragment(FavFragment.newInstance()));
        profileTextView.setOnClickListener(v -> switchFragment(ProfileFragment.newInstance()));

        // 加载主题设置
        int themeMode = ThemeManager.getThemeMode(this);
        AppCompatDelegate.setDefaultNightMode(themeMode);
        // ...
    }

    // 抽取 fragment 替换的逻辑
    private void switchFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}